﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class ActivityMaster : System.Web.UI.Page
{
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    private DataSet GetDataSet()
    {

        DataSet objDs=null;
        try
        {
            objDs = new ActivityMasterBAL().GetActivityMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();
        }
        return objDs;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
           // Session["UserID"] = Guid.NewGuid();
        }
        Clear();

    }
    protected void BtnSearch_OnClick(object sender, EventArgs e)
    {
        hdnInput.Value = "search";
        GetData();
        Clear();
    }
     private void Clear()
    {
        lblErrorMessage.Text = string.Empty;
    }
    protected void BtnClear_OnClick(object sender, EventArgs e)
    {

        txtActivityName.Text = string.Empty;
        lblErrorMessage.Text = string.Empty;
        hdnInput.Value = string.Empty;
        //txtLanguageId.Text = "";
        GetData();
    }
    protected void btnAddNew_OnClick(object sender, EventArgs e)
    {

        //throw new NotImplementedException();

        Response.Redirect("~/Admin/ActivityMasterModify.aspx?agid=0&mod=in");

    }
    protected void gvActivity_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvActivity_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsActive")) == 0)
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to active " + DataBinder.Eval(e.Row.DataItem, "ActivityName") + " Activity?')");
            }
            else
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete " + DataBinder.Eval(e.Row.DataItem, "ActivityName") + " Activity?')");
            }
        }

    }
    protected void gvActivity_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int intActivityID = (int)gvActivity.DataKeys[e.RowIndex].Value;
        string strStatus = Convert.ToString(gvActivity.Rows[e.RowIndex].Cells[4].Text).ToUpper() == "ACTIVE" ? "DELETE" : "ACTIVE";
        DeleteActivity(intActivityID, strStatus);
        GetData();

    }


    private void GetData()
    {
        if (hdnInput.Value == "search")
        {
            DataSet objDs = new DataSet();
            objDs = GetDataSet();
            DataView objDv = new DataView();
            objDv = objDs.Tables[0].DefaultView;
            objDv.RowFilter = "ActivityName  LIKE '*" + txtActivityName.Text.Trim() + "*'";
            gvActivity.DataSource = objDv;
        }
        else
        {
            gvActivity.DataSource = GetDataSet();
        }
        gvActivity.DataBind();
    }


    protected void gvActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvActivity.PageIndex = e.NewPageIndex;
        GetData();
        Clear();
    }
    protected void txtActivityName_TextChanged(object sender, EventArgs e)
    {

    }
    private void DeleteActivity(int intActivity, string strSetMethod)
    {
        ActivityMasterBAL objAMBal;
        ActivityMasterBO objAMBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        string strmod = Convert.ToString(Request.QueryString["mod"]);
        string strReturnValue;


        objAMBal = new ActivityMasterBAL();
        objAMBO = new ActivityMasterBO();

        objAMBO.SetMethod = strSetMethod;
        objAMBO.ActivityID = intActivity;
        objAMBO.ModifyBy = gudUserID;
        objAMBO.ModifyDate = System.DateTime.Today;

        objAMBO.IsActive = (strSetMethod == "ACTIVE" ? true : false);
        strReturnValue = (string)objAMBal.SetActivityMaster(objAMBO);
        if (strReturnValue == "SUCCESS")
        {
            lblErrorMessage.Text = "Record has been updated succesfully.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblErrorMessage.Text = "Error while saving record.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
}